/*     */ package xin.petstore.framework.cas;
/*     */ 
/*     */

/*     */ import javax.servlet.ServletRequest;
/*     */ import javax.servlet.ServletResponse;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ import org.apache.commons.lang3.StringUtils;
/*     */ import org.apache.shiro.authc.AuthenticationException;
/*     */ import org.apache.shiro.authc.AuthenticationToken;
/*     */ import org.apache.shiro.subject.Subject;
/*     */ import org.apache.shiro.web.filter.authc.AuthenticatingFilter;
/*     */ import org.apache.shiro.web.util.WebUtils;
/*     */ import org.slf4j.Logger;
/*     */ import org.slf4j.LoggerFactory;
/*     */ 
/*     */ public class FormAuthenticationFilter extends AuthenticatingFilter
/*     */ {
/*     */   public static final String DEFAULT_ERROR_KEY_ATTRIBUTE_NAME = "shiroLoginFailure";
/*     */   public static final String DEFAULT_USERNAME_PARAM = "username";
/*     */   public static final String DEFAULT_PASSWORD_PARAM = "password";
/*     */   public static final String DEFAULT_REMEMBER_ME_PARAM = "rememberMe";
/*  73 */   private static final Logger log = LoggerFactory.getLogger(FormAuthenticationFilter.class);
/*     */   private String popUrl;
/*  77 */   private String usernameParam = "username";
/*  78 */   private String passwordParam = "password";
/*  79 */   private String rememberMeParam = "rememberMe";
/*     */ 
/*  81 */   private String failureKeyAttribute = "shiroLoginFailure";
/*     */ 
/*     */   public FormAuthenticationFilter() {
/*  84 */     setLoginUrl("/login.jsp");
/*     */   }
/*     */ 
/*     */   public String getPopUrl() {
/*  88 */     return this.popUrl;
/*     */   }
/*     */ 
/*     */   public void setPopUrl(String popUrl) {
/*  92 */     this.popUrl = popUrl;
/*     */   }
/*     */ 
/*     */   public void setLoginUrl(String loginUrl)
/*     */   {
/*  97 */     String previous = getLoginUrl();
/*  98 */     if (previous != null) {
/*  99 */       this.appliedPaths.remove(previous);
/*     */     }
/* 101 */     super.setLoginUrl(loginUrl);
/* 102 */     if (log.isTraceEnabled()) {
/* 103 */       log.trace("Adding login url to applied paths.");
/*     */     }
/* 105 */     this.appliedPaths.put(getLoginUrl(), null);
/*     */   }
/*     */ 
/*     */   public String getUsernameParam() {
/* 109 */     return this.usernameParam;
/*     */   }
/*     */ 
/*     */   public void setUsernameParam(String usernameParam)
/*     */   {
/* 119 */     this.usernameParam = usernameParam;
/*     */   }
/*     */ 
/*     */   public String getPasswordParam() {
/* 123 */     return this.passwordParam;
/*     */   }
/*     */ 
/*     */   public void setPasswordParam(String passwordParam)
/*     */   {
/* 133 */     this.passwordParam = passwordParam;
/*     */   }
/*     */ 
/*     */   public String getRememberMeParam() {
/* 137 */     return this.rememberMeParam;
/*     */   }
/*     */ 
/*     */   public void setRememberMeParam(String rememberMeParam)
/*     */   {
/* 151 */     this.rememberMeParam = rememberMeParam;
/*     */   }
/*     */ 
/*     */   public String getFailureKeyAttribute() {
/* 155 */     return this.failureKeyAttribute;
/*     */   }
/*     */ 
/*     */   public void setFailureKeyAttribute(String failureKeyAttribute) {
/* 159 */     this.failureKeyAttribute = failureKeyAttribute;
/*     */   }
/*     */ 
/*     */   protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
/* 163 */     HttpServletRequest req = (HttpServletRequest)request;
/*     */ 
/* 168 */     if (isLoginRequest(request, response)) {
/* 169 */       if (isLoginSubmission(request, response)) {
/* 170 */         if (log.isTraceEnabled()) {
/* 171 */           log.trace("Login submission detected.  Attempting to execute login.");
/*     */         }
/* 173 */         return executeLogin(request, response);
/*     */       }
/* 175 */       if (log.isTraceEnabled()) {
/* 176 */         log.trace("Login page view.");
/*     */       }
/*     */ 
/* 180 */       return true;
/*     */     }
/*     */ 
/* 183 */     if (log.isTraceEnabled()) {
/* 184 */       log.trace("Attempting to access a path which requires authentication.  Forwarding to the Authentication url [" + 
/* 185 */         getLoginUrl() + "]");
/*     */     }
/* 187 */     String url = WebUtils.getPathWithinApplication(WebUtils.toHttp(request));
/* 188 */     setLoginUrl(getLoginUrl().replaceAll("&decorator=remote", ""));
/* 189 */     int index = getLoginUrl().indexOf("&v=");
/*     */     String _url;
/*     */     String _url;
/* 191 */     if (index > 0)
/* 192 */       _url = getLoginUrl().substring(0, index);
/*     */     else {
/* 194 */       _url = getLoginUrl();
/*     */     }
/* 196 */     if ((this.popUrl != null) && (this.popUrl.contains(url)))
/* 197 */       setLoginUrl(_url + "&decorator=remote&v=" + System.currentTimeMillis());
/*     */     else {
/* 199 */       setLoginUrl(_url + "&v=" + System.currentTimeMillis());
/*     */     }
/* 201 */     String cas = req.getHeader("cas");
/* 202 */     if (StringUtils.isNotEmpty(cas)) {
/* 203 */       HttpServletResponse res = (HttpServletResponse)response;
/* 204 */       res.setHeader("cas", getLoginUrl());
/* 205 */       return false;
/*     */     }
/* 207 */     saveRequestAndRedirectToLogin(request, response);
/* 208 */     return false;
/*     */   }
/*     */ 
/*     */   protected boolean isLoginSubmission(ServletRequest request, ServletResponse response)
/*     */   {
/* 221 */     return ((request instanceof HttpServletRequest)) && (WebUtils.toHttp(request).getMethod().equalsIgnoreCase("POST"));
/*     */   }
/*     */ 
/*     */   protected AuthenticationToken createToken(ServletRequest request, ServletResponse response) {
/* 225 */     String username = getUsername(request);
/* 226 */     String password = getPassword(request);
/* 227 */     return createToken(username, password, request, response);
/*     */   }
/*     */ 
/*     */   protected boolean isRememberMe(ServletRequest request) {
/* 231 */     return WebUtils.isTrue(request, getRememberMeParam());
/*     */   }
/*     */ 
/*     */   protected boolean onLoginSuccess(AuthenticationToken token, Subject subject, ServletRequest request, ServletResponse response) throws Exception
/*     */   {
/* 236 */     issueSuccessRedirect(request, response);
/*     */ 
/* 238 */     return false;
/*     */   }
/*     */ 
/*     */   protected boolean onLoginFailure(AuthenticationToken token, AuthenticationException e, ServletRequest request, ServletResponse response)
/*     */   {
/* 243 */     setFailureAttribute(request, e);
/*     */ 
/* 245 */     return true;
/*     */   }
/*     */ 
/*     */   protected void setFailureAttribute(ServletRequest request, AuthenticationException ae) {
/* 249 */     String className = ae.getClass().getName();
/* 250 */     request.setAttribute(getFailureKeyAttribute(), className);
/*     */   }
/*     */ 
/*     */   protected String getUsername(ServletRequest request) {
/* 254 */     return WebUtils.getCleanParam(request, getUsernameParam());
/*     */   }
/*     */ 
/*     */   protected String getPassword(ServletRequest request) {
/* 258 */     return WebUtils.getCleanParam(request, getPasswordParam());
/*     */   }
/*     */ }

/* Location:           C:\java代码反编译器\core-1.1.4.jar
 * Qualified Name:     com.eloancn.framework.cas.FormAuthenticationFilter
 * JD-Core Version:    0.6.0
 */